// var i;
// var $uI_BoxIitemInfo_UIBindings = require("./UI_BoxIitemInfo_UIBindings");
// var $uIView = require("./UIView");
// var $excel = require("./Excel");
// var $playerDataManager = require("./PlayerDataManager");
// var $bundleConfig = require("./BundleConfig");
// var $commonUtils = require("./CommonUtils");
// var $const_Hall = require("./Const_Hall");
// var _ = cc._decorator;
// var y = _.ccclass;
// var m = _.property;
// var v = _.inspector;
// var b = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.auto_ui = null;
//         t.spriteMaterial = [];
//         return t;
//     }
//     __extends(t, e);
//     t.prototype._show = function (e) {
//         this.auto_ui.node_cardFather.children.forEach(function (t, o) {
//             t.active = o + 1 == e;
//         });
//         var t = [];
//         $excel.Excel.shuiguo_all.forEach(function (o) {
//             if (o.quality == e && -1 != o.isShow) {
//                 t.push({
//                     id: o.id,
//                     isUnLock: $playerDataManager.PlayerDataMgr.GetEquipDataById(o.id).isUnLock
//                 });
//             }
//         });
//         t.sort(function (e, t) {
//             if (e.isUnLock) {
//                 return t.isUnLock ? e.id - t.id : -1;
//             } else {
//                 return 1;
//             }
//         });
//         var o = t.filter(function (e) {
//             return e.isUnLock;
//         });
//         if (0 == o.length) {
//             this.auto_ui.txt_jiLvLabel.string = "每个水果碎片的概率:0%";
//         } else {
//             this.auto_ui.txt_jiLvLabel.string = "每个水果碎片的概率:" + (100 / o.length).toFixed(2) + "%";
//         }
//         this.auto_ui.node_itemFather.children.forEach(function (e) {
//             return (e.active = !1);
//         });
//         for (var n = 0; n < t.length; n++) {
//             t[n];
//             var i = void 0;
//             if (n > this.auto_ui.node_itemFather.childrenCount) {
//                 i = this.auto_ui.node_itemFather.children[n];
//             } else {
//                 (i = cc.instantiate(this.auto_ui.node_ItemIcon)).parent = this.auto_ui.node_itemFather;
//             }
//             i.active = !0;
//             this.ShowItem(i, t[n]);
//         }
//     };
//     t.prototype._clickListener = function () {
//         this._close();
//     };
//     t.prototype.ShowItem = function (e, t) {
//         return __awaiter(this, void 0, void 0, function () {
//             var o;
//             var n = this;
//             return __generator(this, function () {
//                 this._loadRes(
//                     $bundleConfig.BundleNames.Common,
//                     "res/player/player" + (t.id - 1e3) + "_1",
//                     cc.SpriteFrame
//                 ).then(function (o) {
//                     var i = e.getChildByName("icon").getComponent(cc.Sprite);
//                     i.spriteFrame = o;
//                     $commonUtils.default.LimitSpriteMaxSize(i, $const_Hall.equipIconSize);
//                     i.setMaterial(0, t.isUnLock ? n.spriteMaterial[0] : n.spriteMaterial[1]);
//                 });
//                 o = $excel.Excel.shuiguo(t.id).quality;
//                 if (!t.isUnLock) {
//                     o = 0;
//                 }
//                 this._loadRes($bundleConfig.BundleNames.Common, "res/quality/quality_" + o, cc.SpriteFrame).then(
//                     function (t) {
//                         e.getChildByName("quality").getComponent(cc.Sprite).spriteFrame = t;
//                     }
//                 );
//                 e.getChildByName("lock").active = !t.isUnLock;
//                 return [2];
//             });
//         });
//     };
//     __decorate([m($uI_BoxIitemInfo_UIBindings.default)], t.prototype, "auto_ui", void 0);
//     __decorate([m([cc.Material])], t.prototype, "spriteMaterial", void 0);
//     return __decorate([y], t);
// })($uIView.default);
// exports.default = b;


// import UI_BoxIitemInfo_UIBindings from "./UI_BoxIitemInfo_UIBindings";
// import UIView from "./UIView";
// import { Excel } from "./Excel";
// import { PlayerDataMgr } from "./PlayerDataManager";
// import { BundleNames } from "./BundleConfig";
// import CommonUtils from "./CommonUtils";
// import { equipIconSize } from "./Const_Hall";

// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * 建议类名: BoxItemInfo
//  */
// @ccclass
// class UI_BoxIitemInfo extends UIView {
//     @property(UI_BoxIitemInfo_UIBindings)
//     auto_ui: UI_BoxIitemInfo_UIBindings = null;

//     @property([cc.Material])
//     spriteMaterial: cc.Material[] = [];

//     constructor() {
//         super();
//     }

//     /**
//      * 显示指定品质的水果碎片信息
//      * @param quality 水果碎片的品质
//      * 建议方法名: showFruitPiecesByQuality
//      */
//     _show(quality: number): void {
//         this.auto_ui.node_cardFather.children.forEach((child, index) => {
//             child.active = index + 1 === quality;
//         });

//         const fruitPieces = Excel.shuiguo_all.filter(piece => piece.quality === quality && piece.isShow !== -1)
//             .map(piece => ({
//                 id: piece.id,
//                 isUnLock: PlayerDataMgr.GetEquipDataById(piece.id).isUnLock
//             }));

//         fruitPieces.sort((a, b) => {
//             if (a.isUnLock) {
//                 return b.isUnLock ? a.id - b.id : -1;
//             } else {
//                 return 1;
//             }
//         });

//         const unlockedPieces = fruitPieces.filter(piece => piece.isUnLock);
//         if (unlockedPieces.length === 0) {
//             this.auto_ui.txt_jiLvLabel.string = "每个水果碎片的概率:0%";
//         } else {
//             this.auto_ui.txt_jiLvLabel.string = `每个水果碎片的概率:${(100 / unlockedPieces.length).toFixed(2)}%`;
//         }

//         this.auto_ui.node_itemFather.children.forEach(child => {
//             child.active = false;
//         });

//         for (let i = 0; i < fruitPieces.length; i++) {
//             let itemNode: cc.Node;
//             if (i < this.auto_ui.node_itemFather.childrenCount) {
//                 itemNode = this.auto_ui.node_itemFather.children[i];
//             } else {
//                 itemNode = cc.instantiate(this.auto_ui.node_ItemIcon);
//                 itemNode.parent = this.auto_ui.node_itemFather;
//             }
//             itemNode.active = true;
//             this.ShowItem(itemNode, fruitPieces[i]);
//         }
//     }

//     /**
//      * 点击监听器，关闭当前界面
//      * 建议方法名: clickListener
//      */
//     _clickListener(): void {
//         this._close();
//     }

//     /**
//      * 显示单个水果碎片的信息
//      * @param itemNode 碎片节点
//      * @param piece 碎片信息
//      * 建议方法名: showSingleFruitPiece
//      */
//     async ShowItem(itemNode: cc.Node, piece: { id: number, isUnLock: boolean }): Promise<void> {
//         const spriteFrame = await this._loadRes(
//             BundleNames.Common,
//             `res/player/player${piece.id - 1000}_1`,
//             cc.SpriteFrame
//         );

//         const iconSprite = itemNode.getChildByName("icon").getComponent(cc.Sprite);
//         iconSprite.spriteFrame = spriteFrame;
//         CommonUtils.LimitSpriteMaxSize(iconSprite, equipIconSize);
//         iconSprite.setMaterial(0, piece.isUnLock ? this.spriteMaterial[0] : this.spriteMaterial[1]);

//         let quality = Excel.shuiguo(piece.id).quality;
//         if (!piece.isUnLock) {
//             quality = 0;
//         }

//         const qualitySpriteFrame = await this._loadRes(
//             BundleNames.Common,
//             `res/quality/quality_${quality}`,
//             cc.SpriteFrame
//         );

//         itemNode.getChildByName("quality").getComponent(cc.Sprite).spriteFrame = qualitySpriteFrame;
//         itemNode.getChildByName("lock").active = !piece.isUnLock;
//     }
// }

// export default UI_BoxIitemInfo;

import UIBoxIitemInfoUIBindings from "./UI_BoxIitemInfo_UIBindings";
import UIViewBase from "./UIView";
import { Excel } from "./Excel";
import { PlayerDataMgr } from "./PlayerDataManager";
import { BundleNames } from "./BundleConfig";
import CommonUtils from "./CommonUtils";
import { equipIconSize } from "./Const_Hall";

const { ccclass, property } = cc._decorator;

/**
 * 建议类名：BoxItemInfo
 */
@ccclass
class ViewBoxIitemInfo extends UIViewBase {
    @property(UIBoxIitemInfoUIBindings)
    auto_ui: UIBoxIitemInfoUIBindings = null;

    @property([cc.Material])
    spriteMaterial: cc.Material[] = [];

    constructor() {
        super();
    }

    /**
     * 显示指定品质的水果碎片信息
     * @param quality 水果碎片的品质
     * 建议方法名: showFruitPiecesByQuality
     */
    _show(fruitQuality: number): void {
        const cardChildren = this.auto_ui.node_cardFather.children;
        cardChildren.forEach((nodeCardItem, idx) => {
            nodeCardItem.active = idx + 1 === fruitQuality;
        });

        const fruitPieces = Excel.shuiguo_all.filter(fruitPiece => fruitPiece.quality === fruitQuality && fruitPiece.isShow !== -1)
            .map(fruitPiece => {
                const isUnLock = PlayerDataMgr.getEquipDataById(fruitPiece.id).isUnLock;
                return { id: fruitPiece.id, isUnLock: isUnLock };
            });

        fruitPieces.sort((a, b) => {
            if (a.isUnLock) {
                return b.isUnLock ? a.id - b.id : -1;
            } else {
                return 1;
            }
        });

        const unlockedPieces = fruitPieces.filter(fruitPiece => fruitPiece.isUnLock);
        this.auto_ui.txt_jiLvLabel.string = unlockedPieces.length === 0 
            ? "每个水果碎片的概率:0%" 
            : `每个水果碎片的概率:${(100 / unlockedPieces.length).toFixed(2)}%`;

        const itemFatherChildren = this.auto_ui.node_itemFather.children;
        itemFatherChildren.forEach(childNode => {
            childNode.active = false;
        });
        
        for (let i = 0; i < fruitPieces.length; i++) {
            let newItemNode: cc.Node;
            if (i < this.auto_ui.node_itemFather.childrenCount) {
                newItemNode = this.auto_ui.node_itemFather.children[i];
            } else {
                newItemNode = cc.instantiate(this.auto_ui.node_ItemIcon);
                newItemNode.parent = this.auto_ui.node_itemFather;
            }
            newItemNode.active = true;
            this.showItem(newItemNode, fruitPieces[i]);
        }
    }

    /**
     * 点击监听器，关闭当前界面
     * 建议方法名: clickListener
     */
    _clickListener(): void {
        this._close();
    }

    /**
     * 显示单个水果碎片的信息
     * @param itemNode 碎片节点
     * @param piece 碎片信息
     * 建议方法名: showSingleFruitPiece
     */
    async showItem(fruitItemNode: cc.Node, fruitPiece: { id: number, isUnLock: boolean }): Promise<void> {
        const iconSpriteFrame = await this._loadRes(
            BundleNames.Common,
            `res/player/player${fruitPiece.id - 1000}_1`,
            cc.SpriteFrame
        );

        const iconSpriteComponent = fruitItemNode.getChildByName("icon").getComponent(cc.Sprite);
        iconSpriteComponent.spriteFrame = iconSpriteFrame;
        CommonUtils.limitSpriteSize(iconSpriteComponent, equipIconSize);
        iconSpriteComponent.setMaterial(0, fruitPiece.isUnLock ? this.spriteMaterial[0] : this.spriteMaterial[1]);

        let pieceQuality = Excel.shuiguo(fruitPiece.id).quality;
        if (!fruitPiece.isUnLock) {
            pieceQuality = 0;
        }

        const qualitySpriteFrame = await this._loadRes(
            BundleNames.Common,
            `res/quality/quality_${pieceQuality}`,
            cc.SpriteFrame
        );

        fruitItemNode.getChildByName("quality").getComponent(cc.Sprite).spriteFrame = qualitySpriteFrame;
        fruitItemNode.getChildByName("lock").active = !fruitPiece.isUnLock;
    }
}

export default ViewBoxIitemInfo;